package com.open.web.config;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * @author: codex
 * @Date: 2024/7/19 下午4:12
 * @Version: V1.0
 * @description: desc:
 */

@Configuration
public class LocalDateTimeSerializerConfig {

    @Bean
    public LocalDateTimeSerializer localDateTimeSerializer(JacksonProperties properties) {
        String dateFormat = properties.getDateFormat();
        if (!StringUtils.hasText(dateFormat)) {
            dateFormat = "yyyy-MM-dd HH:mm:ss";
        }
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateFormat));
    }

    @Bean
    public LocalDateTimeDeserializer localDateTimeDeserializer(JacksonProperties properties) {
        String dateFormat = properties.getDateFormat();
        if (!StringUtils.hasText(dateFormat)) {
            dateFormat = "yyyy-MM-dd HH:mm:ss";
        }
        return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateFormat));

    }

    // @Bean  和com.open.xss.config.XssConfig.jackson2ObjectMapperBuilderCustomizer重复
    // public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(
    //         LocalDateTimeSerializer localDateTimeSerializer, LocalDateTimeDeserializer localDateTimeDeserializer) {
    //     return builder -> {
    //         // 序列化
    //         builder.serializerByType(LocalDateTime.class, localDateTimeSerializer);
    //         // 反序列化
    //         builder.deserializerByType(LocalDateTime.class, localDateTimeDeserializer);
    //     };
    // }

}
